package com.furschina.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.wooshai.core.query.Expression;
import cn.wooshai.core.query.Query;
import cn.wooshai.core.query.QueryFactory;
import cn.wooshai.core.service.local.AbstractService;

import com.furschina.dao.ChannelDao;
import com.furschina.model.Channel;
import com.furschina.vo.ChannelPage;

@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class ChannelService extends AbstractService<ChannelDao, Channel> {
	public Collection<Channel> findAll(ChannelPage page) {
		Query q = QueryFactory.createQuery(Channel.class).orderBy("sort");
		List<Expression> exps = new ArrayList<Expression>();
		if (page.isRoot()) {
			exps.add(Expression.isNull("parent"));
		} else {
			exps.add(Expression.isNotNull("parent"));
		}
		if (!exps.isEmpty()) {
			q.where(Expression.and(exps));
		}
		return this.queryAll(q, page, true);
	}
}
